﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Hosting;
using System.Xml;

/// <summary>
/// WxText 的摘要说明
/// </summary>
public class WxText
{
    public static string GetWxTextXml(string StrXml)
    {
        string result = string.Empty;
        //加载xml
        XmlDocument textXml = new XmlDocument();
        textXml.LoadXml(StrXml);
        XmlElement xmlElement = textXml.DocumentElement;
        //转成model对象

        string ToUserName = xmlElement.SelectSingleNode("ToUserName").InnerText;
        string FromUserName = xmlElement.SelectSingleNode("FromUserName").InnerText;
        string CreateTime = xmlElement.SelectSingleNode("CreateTime").InnerText;
        string MsgType = xmlElement.SelectSingleNode("MsgType").InnerText;
        string Content = xmlElement.SelectSingleNode("Content").InnerText;
        string MsgId = xmlElement.SelectSingleNode("MsgId").InnerText;


        return ToUserName;
    }

    /// <summary>
    /// 记录日志
    /// </summary>
    /// <param name="log">日志内容</param>
    public static void wxLog(string log)
    {
        string logPath = HostingEnvironment.MapPath("~") + "\\log1.txt";
        if (!File.Exists(logPath))
        {
            File.Create(logPath).Close();
        }

        using (StreamWriter sw = new StreamWriter(logPath, true, Encoding.UTF8))
        {
            sw.WriteLine(log);
            sw.Close();
        }

    }

    public static string ConvertXmlToString(XmlDocument xmlDoc)
    {
        MemoryStream stream = new MemoryStream();
        XmlTextWriter writer = new XmlTextWriter(stream, null);
        writer.Formatting = Formatting.Indented;
        xmlDoc.Save(writer);
        StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);
        stream.Position = 0;
        string xmlString = sr.ReadToEnd();
        sr.Close();
        stream.Close();
        return xmlString;
    }

}